package com.example.diet_sport.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.diet_sport.entity.sport;
import com.example.diet_sport.entity.user;
import com.example.diet_sport.entity.usercalorie;
import com.example.diet_sport.entity.userfood;
import com.example.diet_sport.mapper.sportMapper;
import com.example.diet_sport.mapper.userMapper;
import com.example.diet_sport.mapper.usercalorieMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 伽蓝尘
 * @create 2023-06-08 10:03
 */
@RequestMapping("/usercalorie")
@RestController
@CrossOrigin
public class usercalorieController {
    @Autowired
    private usercalorieMapper usercalorieMapper;
    @Autowired
    private userController usercon;



    /**
     * 查询用户某天热量
     * @return
     */
    @ResponseBody
    @GetMapping("/qurry")
    public usercalorie qurryUserDay(int uid,String day){
        QueryWrapper<usercalorie> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        wrapper.eq("day",day);
        List<usercalorie> list = usercalorieMapper.selectList(wrapper);
        System.out.println(list);
        if(!list.isEmpty()){
            return list.get(0);
        }else {
            return null;
        }

    }


    /**
     * 获得用户七天卡路里double数组
     * @return
     */
    @ResponseBody
    @GetMapping("/calorieList")
    public double[] getUserSevendayList(int uid){
        QueryWrapper<usercalorie> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        List<usercalorie> list= usercalorieMapper.selectList(wrapper);
        double[] show= new double[7];
        String[] or = sevenDays();
        int i = 0;
        for (usercalorie a:list) {
            for (String b:or) {
                if(a.getDay().equals(b)){
                    show[i++]=a.getCalorie();
                }
            }
        }
        return show;
    }


    /**
     * 获得用户七天的热量统计list
     * @return
     */
    @ResponseBody
    @GetMapping("/usersevenday")
    public List<usercalorie> sevenDay(int uid){
        QueryWrapper<usercalorie> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        List<usercalorie> list= usercalorieMapper.selectList(wrapper);
        List<usercalorie> show= new ArrayList<usercalorie>();
        String[] or = sevenDays();
        for (usercalorie a:list) {
            for (String b:or) {
                if(a.getDay().equals(b)){
                    show.add(a);
                }
            }
        }
        return show;
    }


    /**
     * 获得七天的日期
     * @return
     */
    public String[]sevenDays(){
        SimpleDateFormat s1= new SimpleDateFormat("dd");
        SimpleDateFormat s2= new SimpleDateFormat("mm");
        int day = Integer.parseInt(s1.format(System.currentTimeMillis()));
        int month =  Integer.parseInt(s2.format(System.currentTimeMillis()));
        int sevenday = day-6;
        String head = "2023.6.";
        String[] or = new String[7];
        int con = 7 ;
        for(int i = 0;i<7;i++){
            or[i] = head + sevenday;
            sevenday++;
        }
        return or;
    }

    /**
     * 添加一天卡路里的记录
     * @return
     */
    public int add(int uid ,String day){
        usercalorie usercalorie = new usercalorie(uid,day);
        return usercalorieMapper.insert(usercalorie);
    }

    /**
     *更新每日已摄入以及可摄入热量
     */
    @ResponseBody
    @PostMapping("/update")
    public int update(int uid,String day,double calorie,int fors){
        user user = usercon.queryById(uid);
        usercalorie usercalorie = qurryUserDay(uid,day);
        if(null==usercalorie){
            add(uid,day);
            usercalorie = qurryUserDay(uid,day);
        }
        if(usercalorie.getNewin()==0){
            usercalorie.setRest(user.getIntake());
            usercalorie.setNewin(1);
        }
        double calorieHas = usercalorie.getCalorie();
        double rest = usercalorie.getRest();
        //fors:0:食物摄入；1:运动消耗
        if(fors==0){
            usercalorie.setCalorie(calorieHas+calorie);
            usercalorie.setRest(rest-calorie);
        }
        else {
            usercalorie.setCalorie(calorieHas-calorie);
        }
        return usercalorieMapper.updateById(usercalorie);
    }



    /**
     * 卡路里列表
     * @return
     */
    @ResponseBody
    @GetMapping("/getlist")
    public List<usercalorie> qurryCalorie() {
        return  usercalorieMapper.selectList(null);}


}
